package main

import (
	"fmt"
	"sync"
)

var (
	sum int
	//mutex sync.Mutex
	mutex sync.RWMutex
)

func main() {
	var wg sync.WaitGroup
	wg.Add(110)
	for i := 1; i <= 100; i++ {

		go func() {
			defer wg.Done()
			add(10)
		}()

	}
	for i := 1; i <= 10; i++ {

		go func() {
			defer wg.Done()
			fmt.Println("sum的值：", readSum())
		}()
	}
	wg.Wait()
	fmt.Print("sum:", sum)
}
func add(num int) {
	mutex.Lock()
	defer mutex.Unlock()
	sum += num
}

func readSum() int {
	mutex.RLock()
	defer mutex.RUnlock()
	b := sum
	return b
}
